package com.it.a09threadsafe1;

public class MyThread extends Thread {
    static int ticket = 0;
    //锁对象，一定要是唯一的。
    static Object obj = new Object();
    public void run() {
        while (true) {
            //同步代码块
            synchronized (obj) {
                if (ticket < 100) {
                    ticket++;
                    try {
                        Thread.sleep(10);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    System.out.println(getName() + "买了"+ticket+"张票！！！");
                }else{
                    break;
                }
            }
        }
    }
}
